package com.cac.demo.controller.tsUser;


import com.cac.demo.dao.tsUser.TsUserDao;
import com.cac.demo.entity.tsUSer.TsUserDTO;
import com.cac.demo.service.tsUSer.TsUserService;
import com.cac.demo.util.AccessLog;
import com.cac.demo.util.ResultGenerator;

import com.cac.demo.util.adapter.ResponseMsg;
import io.swagger.annotations.Api;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/")
@Api(tags = "系统管理")
public class LoginController {
    @Autowired
    private TsUserService tsUserService;
    @Autowired
    private ResultGenerator generator;
    @Autowired
    private TsUserDao tsUserDao;


    /**
     * @param map
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/changePassword")
    public HashMap<String, Object> changePassword(@RequestBody Map map, HttpServletRequest request, HttpServletResponse response) {
        String oldPwd = map.get("oldPwd").toString();
        String newPwd = map.get("newPwd").toString();

        HttpSession session = request.getSession();
        HashMap<String, Object> mapData = new HashMap<>();
//        ResponseMsg<TsUserDTO> responseMsg = new ResponseMsg<>();
//        String sessionId=session.getId();

        System.out.println("session==========" + session);
//        String account = (String)map.get("account");
        String account = (String) session.getAttribute("account");
        String password = DigestUtils.md5Hex(newPwd);
        int rsl = tsUserService.searchOldPwd(DigestUtils.md5Hex(oldPwd), account);
        Integer data = 0;
        if (rsl == 1) {
            data = tsUserService.changePassword(account, password, newPwd);
            if (data == 0) {
                mapData.put("retCode", "-1");
                mapData.put("message", "修改失败！");
            } else {
                mapData.put("retCode", "200");
                mapData.put("message", "修改成功,请重新登陆！");
            }
        } else {
            mapData.put("retCode", "-1");
            mapData.put("message", "输入的旧密码错误！");
        }
        return mapData;
    }

    /*
     * @Description:登录校验
     * @Param:account:用户名
     * @Param:password:密码
     * @Param:session:session域
     * @Param:attributes返回页面消息
     * @Return:登录成功跳转登录成功页面，登录失败返回登录页面
     * */
    /*@RequestParam(value = "account") String account,
    @RequestParam(value = "password") String password*/
    @PostMapping("/login")
    @AccessLog(LogType = "登录", sendOrReceive = "receiveTxt")
    public ResponseMsg<TsUserDTO> login(@RequestBody Map map, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        ResponseMsg<TsUserDTO> responseMsg = new ResponseMsg<>();
        String account = (String) map.get("account");
        String password = (String) map.get("password");
        String IP = null;
        request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }

       /* if(account.equals("274099")){
            int countIp = tsUserDao.searchAdministratorIp(IP);
            if(countIp == 0){
                responseMsg.setRetCode("400");
                responseMsg.setMessage("管理员账号无法用于该电脑登录！");
                return responseMsg;
            }
        }*/
        //验证账户
        TsUserDTO tsuser = tsUserService.checkUser(account, password);
        if (tsuser != null) {
            String sessionId = session.getId();
            session.setMaxInactiveInterval(-1);
            response.setHeader("session", request.getSession().getId());
            responseMsg.setMessage(sessionId);
            responseMsg.setRetCode("200");
            /*
            Cookie cookie=new Cookie("SessionId",sessionId);
            cookie.setPath(request.getContextPath());
            response.addCookie(cookie);*/
            request.getSession().setAttribute("tsUser", tsuser);
            request.getSession().setAttribute("account", account);
            responseMsg.setResponseBody(tsuser);
            // responseMsg.setResponseBody(session);
            return responseMsg;
        } else {
            responseMsg.setRetCode("400");
            responseMsg.setMessage("用户名或密码错误！");
            return responseMsg;
        }
    }

}
